package com.coding.controller;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.pojo.Theclass;
import com.coding.service.TheclassService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@Controller
@ResponseBody
@RequestMapping("theclass")
public class ClassController {
    @Autowired
    TheclassService theclassService;

    @ApiOperation(value = "无条件分页查询班级信息")
    @RequestMapping(value = "/selectAllByPage", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel selectAllByPage(@ModelAttribute PageQueryModel pageQueryModel) {
        BaseModel model = new BaseModel();
        try {
            theclassService.selectAllbypage(pageQueryModel, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @ApiOperation(value = "搜索班级信息")
    @RequestMapping(value = "/searchClass", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel searchClass(@ApiParam(value = "user_id", required = true) @RequestParam("user_id") int user_id,
                                 @ApiParam(value = "搜索内容", required = true) @RequestParam("text") String text,
                                 @ModelAttribute PageQueryModel pageQueryModel) {
        BaseModel model = new BaseModel();
        try {
            theclassService.searchClass(user_id, text, pageQueryModel, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @ApiOperation(value = "查询单个班级信息")
    @RequestMapping(value = "/selectById", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel selectById(@ApiParam(value = "班级id", required = true) @RequestParam("class_id") int class_id
    ) {
        BaseModel model = new BaseModel();
        try {
            theclassService.selectbyclass_id(class_id, model);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return model;
    }

    @ApiOperation(value = "通过标签查询")
    @RequestMapping(value = "/selectByTag", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel selectbytag(@ApiParam(value = "班级标签", required = true) @RequestParam("tag") String tag,
                                 @ModelAttribute PageQueryModel pageQueryModel) {
        BaseModel model = new BaseModel();
        try {
            theclassService.selectbytag(tag, pageQueryModel, model);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return model;
    }

    @ApiOperation(value = "新增班级")
    @RequestMapping(value = "/insert", method = RequestMethod.POST, consumes = "multipart/*", headers = "content-type=multipart/form-data")
    public BaseModel insert(@ApiParam(value = "班级name", required = true) @RequestParam("class_name") String class_name,
                            @ApiParam(value = "班级老师", required = false, defaultValue = "10") @RequestParam("teacher_id") int teacher_id,
                            @ApiParam(value = "班级tag", required = false) @RequestParam("tag") String tag,
                            @ApiParam(value = "班级介绍", required = false) @RequestParam("intro") String intro,
                            @ApiParam(value = "上传的图片", required = true) MultipartFile file, HttpServletRequest request) {
        BaseModel model = new BaseModel();
        try {
            Theclass theclass = new Theclass();
            theclass.setClassName(class_name);
            theclass.setClassTeacherId(teacher_id);
            theclass.setClassTag(tag);
            theclass.setClassIntro(intro);
            theclassService.insertnewclass(theclass,file, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @ApiOperation(value = "更新班级信息")
    @RequestMapping(value = "/update", method = {RequestMethod.GET})
    public BaseModel getimagebytag(@ApiParam(value = "班级id", required = true) @RequestParam("class_id") int class_id,
                                   @ApiParam(value = "班级name", required = false) @RequestParam("class_name") String class_name,
                                   @ApiParam(value = "班级老师id", required = false, defaultValue = "1") @RequestParam("teacher_id") int teacher_id,
                                   @ApiParam(value = "班级tag", required = false) @RequestParam("tag") String tag,
                                   @ApiParam(value = "班级介绍", required = false) @RequestParam("intro") String intro
    ) {
        BaseModel model = new BaseModel();
        try {
            Theclass theclass = new Theclass();
            theclass.setClassName(class_name);
            theclass.setClassTeacherId(teacher_id);
            theclass.setClassTag(tag);
            theclass.setClassIntro(intro);
            theclassService.updateclassintro(theclass, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }
}
